package com.fingard.dsp.bank.directbank.citic02;

import com.fingard.dsp.bank.directbank.bankInterface.IRetailStockMsg;
import com.fingard.dsp.bank.reqrespobj.retailstockmsg.ReqRetailStockMsgQry;
import com.fingard.dsp.bank.reqrespobj.retailstockmsg.RetAtsRetailStockMsg;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/10/19.
 */
public class CITIC02RetailStockMsg extends CITIC02Base implements IRetailStockMsg {
    @Override
    public RetAtsRetailStockMsg getRetailStockMsg(ReqRetailStockMsgQry qry) throws Exception {
        RetAtsRetailStockMsg retAts = new RetAtsRetailStockMsg(qry.reqHeader);
        retAts.writeStart();
        try {
            CITIC02Header header =  new CITIC02Header();
            header.stdprocode = "RLTMSG";
            header.msgsendertrc = reqHeader.transSeqID;
            header.trdcode = "CPCROISM";

//            封装body内容
            XmlTextWriter xmlWriterBody = new XmlTextWriter();
            xmlWriterBody.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            xmlWriterBody.writeStartElement("stream");
            xmlWriterBody.writeStartElement("LST");
            int index =0;
            while (qry.readRecord()) {
                xmlWriterBody.writeStartElement("ROW");
//            HOSTNO	企业端经销商客户代码	VARCHAR	20	Y
                xmlWriterBody.writeElementString("HOSTNM", qry.ReqDetail.DealerCode);
//            HOSTNAME	借款企业名称	VARCHAR	122	Y	借款企业名称
                xmlWriterBody.writeElementString("HOSTNAME", qry.ReqDetail.DealerName);
//            ORDERNO	交易流水号	VARCHAR	20	Y	企业端发票交易流水号(批次号)
                xmlWriterBody.writeElementString("ORDERNO", reqHeader.transSeqID+String.format("%03d",index));
//            OPRNO	操作人编号	VARCHAR	20	N
                xmlWriterBody.writeElementString("OPRNO", "");
//            OPRNM	操作人名称	VARCHAR	30	N
                xmlWriterBody.writeElementString("OPRNM", "");
//            YEARTOTALRETAILNUM	年度累计零售车辆总数	VARCHAR	30	N
                xmlWriterBody.writeElementString("YEARTOTALRETAILNUM", qry.ReqDetail.Ndljls);
//            YEARTOTALRETAILMONEY	年度累计零售车辆总金额	VARCHAR	19,2	N
                xmlWriterBody.writeElementString("YEARTOTALRETAILMONEY", qry.ReqDetail.NdljlsMoney);
//            MONTHTOTALRETAILNUM	本月累计零售车辆总数	VARCHAR	30	N
                xmlWriterBody.writeElementString("MONTHTOTALRETAILNUM", qry.ReqDetail.Byljls);
//            MONTHTOTALRETAILMONEY	本月累计零售车辆总金额	DECIMAL	19,2	N
                xmlWriterBody.writeElementString("MONTHTOTALRETAILMONEY", qry.ReqDetail.ByljlsMoney);
//            STOCKCARNUM	实时库存车辆总数	VARCHAR	30	N
                xmlWriterBody.writeElementString("STOCKCARNUM", qry.ReqDetail.Ssckcl);
//            STOCKCARMONEY	实时库存车辆总金额	VARCHAR	19,2	N
                xmlWriterBody.writeElementString("STOCKCARMONEY", qry.ReqDetail.SsckclMoney);
//            FIELD1	属性1	DECIMAL	19,2	N
                xmlWriterBody.writeElementString("FIELD1", "");
//            FIELD2	属性2	VARCHAR	30	N
                xmlWriterBody.writeElementString("FIELD2", "");
//            FIELD3	属性3	VARCHAR	60	N
                xmlWriterBody.writeElementString("FIELD3", "");
//            FIELD4	属性4	VARCHAR	100	N
                xmlWriterBody.writeElementString("FIELD4", "");
//            FIELD5	属性5	VARCHAR	100	N
                xmlWriterBody.writeElementString("FIELD5", "");
//                    LIST_END
                xmlWriterBody.writeEndElement();
            }
            xmlWriterBody.writeEndDocument();
            String[] tmpStrRet = sendToBank2(xmlWriterBody.toXmlString(),header);
            if(tmpStrRet[0].length() == 0) {
                StringBuilder tmpbkMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpbkMsg);
                if (tmpBkReader != null) {
//                    <?xml version="1.0" encoding="UTF-8"?>
//                    <stream>
//                    <RETCODE>AAAAAAA</RETCODE>
//                    </stream>
                    String retCode =tmpBkReader.getFirstText("/stream/RETCODE");
                    if("AAAAAAA".equals(retCode)){
                        retAts.respCode = "0";
                    }else{
                        retAts.respCode = "-1";
                    }
                    retAts.respInfo = retCode;
                }else {
                    throw new Exception(tmpBkReader.toString());
                }
            } else{
                throw new Exception(tmpStrRet[0]);
            }
        }catch (Exception ex){
            ex.getStackTrace();
            retAts.respCode ="-1";
            retAts.respInfo = ex.getMessage();
        }
        retAts.writeRespCode(retAts.respCode);
        retAts.writeRespInfo(retAts.respInfo);
        retAts.writeEnd();
        return retAts;
    }
}
